package com.p2p.backend.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.backend.service.OplogService;
import com.p2p.backend.util.PageUtils;
import com.p2p.base.model.Action;
import com.p2p.base.model.Module;
import com.p2p.base.model.Oplog;
import com.p2p.base.page.PageVo;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.model.MemberPoint;
import com.p2p.front.dal.model.MemberPointLog.TypeEnum;
import com.p2p.front.service.MemberPointService;

@Controller
public class MemberPointController extends BaseController{
	
	@Resource
	private MemberPointService memberPointservice;
	
	@RequestMapping("/backend/memberpoint_list")
	public String userMemeberPointList(PageVo form,Model model) {
		PageVo page = memberPointservice.listAllUserPoint(form);
		model.addAttribute("res", 1);
		model.addAttribute("data", PageUtils.data(page));
		return "/backend/memberpoint_list";
	}

	@RequestMapping("/backend/listlog")
	public String userPOintlogList(PageVo form,Model model,String uid) {
		int index = uid.indexOf("?");
		if(index!=-1) {
			uid = uid.substring(0, index);
		}
		form.getParams().put("user_id", uid);
		PageVo page = memberPointservice.listPage(form);
		model.addAttribute("res", 1);
		model.addAttribute("data", PageUtils.data(page));
		return "/backend/listlog";
	}
	
	@RequestMapping("/editMemberLevel")
	public String editLevel(String uid,Model model, HttpServletRequest request) {
		MemberPoint memberpoint = memberPointservice.getUserMemberPoint(uid);
		model.addAttribute("memberpoint", memberpoint);
		System.out.println(memberpoint.getProduceCumulative());
		model.addAttribute("test",memberpoint.getProduceCumulative());
		return "user/editMemberLevel";
	}
	
	@RequestMapping("/savelevel")
	public void saveLevel(String userId,String level,String modifyMP,String modifytype, HttpServletRequest request) {
		int lev = Integer.parseInt(level);
		MemberPoint memberpoint = memberPointservice.getUserMemberPoint(userId);
		memberpoint.setLevel(lev);
		memberPointservice.update(memberpoint);
		int modifyMpint = Integer.parseInt(modifyMP);
		if(modifyMpint!=0) {	//用户积分人工修改
			if(modifytype.equals("increase")) {
				memberPointservice.cumulative(userId, modifyMpint, TypeEnum.ARTIFICIALITY_PRO);
			} else if(modifytype.equals("reduce")) {
				memberPointservice.cumulative(userId, modifyMpint, TypeEnum.ARTIFICIALITY_CON);
			}
		}
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.MEMBER_POINT);
		oplog.setAction(Action.UPDATE);
		oplog.setParams("");
		OplogService.log(oplog);
	}
}
